import eventBus from "@/typings/GxEventBus";
import { GxTask } from "@/typings/GxTask";
import { message } from "antd";
import { useEffect, useState } from "react";
import TaskModal from "./components/TaskModal";
import TaskInput from "./task-input";
import TaskList from "./task-list";
import taskService from "../../services/TaskService";
import { IFilter } from "@/typings/IFilter";

export interface IPageProps {
  // 当前页记录数
  pageItems?: number;
  // 当前页码数
  pageNo?: number;
  // 每页显示记录数
  pageSize?: number;
  // 总记录数
  totalItems?: number;
  // 总页数
  totalPages?: number;
}

const Task = () => {
   /** 统一定义是否是新增任务还是编辑任务 */
  const [bEdit, setEdit] = useState<boolean>(false);
   /** 统一定义任务查询列表结果 */
  const [tasks, setTasks] = useState<GxTask[]>([]);
   /** 统一定义任务表格加载状态 */
  const [loading, setLoading] = useState<boolean>(true);
   /** 统一定义查询条件 */
  const [filter,setFilter]=useState<IFilter>({pageNum:1,pageSize:8})
  /** 统一定义分页对象 */
  const [page, setPage] = useState<IPageProps>({ pageNo: 1, pageSize: 8 });
  useEffect(() => {
    eventBus.addListener("setTaskEdit", (bEdit: boolean) => {
      setEdit(bEdit);
    });
    eventBus.addListener("setFilter", (filter: IFilter) => {
      setFilter(filter);
    });
    getTasks();
    return () => {
      eventBus.removeListener("setTaskEdit");
    };
  }, []);

  const getTasks = () => {
    /** 查询任务 */
    taskService.query(filter).then((res: any) => {
      if (res.data && res.data.items) {
        let tasks: GxTask[] = [];
        res.data.items.map((item: GxTask, index: number) => {
          let task = new GxTask(item);
          tasks.push(task);
        });
        setPage({
          pageNo: res.data.pageNo,
          pageSize: res.data.pageSize,
          pageItems: res.data.pageItems,
          totalItems: res.data.totalItems,
          totalPages: res.data.totalPages,
        });
        setTasks(tasks);
        setLoading(false);
      }
    });
  };

  const completeTask = (id: number) => {
    taskService
      .complete(id)
      .then((res: any) => {
        message.success("更新成功！");
        getTasks();
      })
      .catch((err: any) => {
        getTasks();
      });
  };

  const deleteTask = (id: number) => {
    taskService.delete(id).then((res: any) => {
      message.success("删除成功！");
      getTasks();
    });
  };

  const onHandleOk = (obj: GxTask) => {
    // 最终新建和更新任务的操作都在父组件中做
    if (!obj) {
      message.error(
        bEdit ? "编辑的任务对象不能为空" : "新建任务对象不允许空！"
      );
    }
    if (bEdit) {
      // 调用update接口
      taskService.update(obj).then((res: any) => {
        if (res.data) {
          message.success("更新成功！");
          eventBus.publish("setVisible", false);
          getTasks();
        }
      });
    } else {
      // 调用create接口
      taskService.save(obj).then((res: any) => {
        if (res.data) {
          message.success("创建成功！");
          eventBus.publish("setVisible", false);
          getTasks();
        }
      });
    }
  };

  return (
    <div className="home-task">
      <TaskInput queryTasks={getTasks} filter={filter}/>
      <hr />
      <TaskList
        taskList={tasks}
        filter={filter}
        page={page}
        isLoading={loading}
        completeTask={completeTask}
        deleteTask={deleteTask}
        queryTasks={getTasks}
      />
      <TaskModal bEdit={bEdit} onHandleOk={onHandleOk} />
    </div>
  );
};

export default Task;
